home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_014 / dimensions / 4d.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  273 lines

  1. /*  This program will display FOUR dimensions on a TWO dimensional screen  */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/exec.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <graphics/gfxbase.h>
  8. #include <graphics/gfx.h>
  9. #include <graphics/text.h>
  10. #include <graphics/regions.h>
  11. #include <graphics/copper.h>
  12. #include <graphics/gels.h>
  13. #include <devices/serial.h>
  14. #include <devices/keymap.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <libraries/dos.h>
  18. #include <math.h>
  19. #ifdef LATTICE        /* Only Lattice currently has limits.h */
  20. #include <limits.h>
  21. #endif
  22. #include <hardware/blit.h>
  23.  
  24. #define INTUITION_REV 1
  25. #define GRAPHICS_REV  1
  26. #define MILLION       1000000
  27.  
  28. struct IntuitionBase *IntuitionBase;
  29. struct GfxBase       *GfxBase;
  30. struct Screen        *Screen;
  31. struct Window        *Window;
  32. struct IntuiMessage  *Message;
  33.  
  34. struct NewScreen NewScreen =
  35.  {
  36.     0,1,640,400,1,1,2,LACE | HIRES, CUSTOMSCREEN,
  37.     NULL,"FOUR DIMENSIONAL POINT MANIPULATION",NULL,NULL,
  38.  };
  39. struct NewWindow NewWindow =
  40.  {
  41.     0,00,639,399,1,1,
  42.     CLOSEWINDOW | MOUSEMOVE | MOUSEBUTTONS,
  43.     WINDOWCLOSE | WINDOWSIZING,
  44.     NULL,NULL,NULL,NULL,NULL,
  45.     0,0,0,0,CUSTOMSCREEN,
  46.  };
  47.  
  48. /* This is the data all the functions need to see */
  49.  
  50. #define TOP 100 /* 100 4d points maximum, change at will       */
  51. #define WID 5   /* Width of the 4d matrix x,y,z,t, perspective */
  52. #define DEW 2   /* Width of display data  x,y                  */
  53. #define PLA 6   /* Six Planes in 4 dimensions                  */
  54. #define SIZ 100 /* Size of each line of capture at maximum     */
  55.  
  56. static double four [ WID ][ WID ];/* This matrix will contain the 4d formula */
  57. static double deta [ TOP ][ WID ];/* This matrix will contain your data      */
  58.           int disp [ TOP ][ WID ];/* Screen coords, note use of WID not DEW  */
  59.           int conn [ 100 ][ 5   ];/* Point connection matrix(NOTE NOT PLANES)*/
  60. static double rads [ PLA ]       ;/* Six rotation planes in four dimensions! */
  61.  
  62. /*********** RETRIEVE DATA FUNCTION ************/
  63.  
  64. retrieve (file)
  65. char *file[];
  66. {
  67.      FILE *input;
  68.      char buffer [ SIZ ],rec [ 20 ]; /* 80 chars on one line, 4 sets */
  69.      int a,i,pos;
  70.      pos = FALSE; /* if it stays false program will abort later */
  71.                   /* else it will represent size of data        */
  72.  
  73.      if ((input = fopen(file,"r"))== NULL) goto HEAVEN;
  74.  
  75.      while ((fgets(buffer,SIZ,input)))
  76.   {
  77.           a = 0;
  78.           for (i=0;i<WID;i++)
  79.        {
  80.                if (pos > TOP) goto HEAVEN;
  81.                a = strcspn(buffer,",");
  82.                strncpy (rec,buffer,a);
  83.                deta [pos][i] = atof (rec);
  84.                ++a;
  85.                strcpy (buffer,buffer+a);
  86.        }
  87.      ++pos;
  88.   }
  89. HEAVEN:
  90.  
  91.      fclose (input);
  92.      return (pos-1);
  93. }
  94.  
  95. /*********** MULTIPLY DATA FUNCTION ************/
  96.  
  97. multiply (data,P1,P2)
  98. int data;
  99. double P1,P2;
  100. {
  101.     int c,r,i;
  102.     float yx,zx,zy,tx,ty,tz;
  103.     float tweedle;
  104.     float Stz,Sty,Stx,Szy,Szx,Syx,Ctz,Cty,Ctx,Czy,Czx,Cyx;
  105.  
  106. /* pull desired rotation radians out of array */
  107.     yx = rads [ 0 ];
  108.     zx = rads [ 1 ];
  109.     zy = rads [ 2 ];
  110.     tx = rads [ 3 ];
  111.     ty = rads [ 4 ];
  112.     tz = rads [ 5 ];
  113.  
  114. /* Pre initialize Sin and Cosine Values, I evaluated and stripped negative Sins */
  115.     Stz=sin(tz);Ctz=cos(tz);
  116.     Sty=sin(ty);Cty=cos(ty);
  117.     Stx=sin(tx);Ctx=cos(tx);
  118.  
  119.     Szy=sin(zy);Czy=cos(zy);
  120.     Szx=sin(zx);Czx=cos(zx);
  121.  
  122.     Syx=sin(yx);Cyx=cos(yx);
  123.  
  124. /*  This is the 4d array, it does all the work
  125.  *  See the program MATFINDER to see how this came into being
  126.  *  At this point we multiply this throught to make a pure numeric -
  127.  *  representation of the 4d matrix for this rotation,
  128.  *  so we can cross multiply it later...
  129.  *  Note again: I stripped all the symbols for negative sin off and -
  130.  *  evaluated those expressions immediately, converting them to positive
  131.  */
  132.     four [0][0] = Ctz*Cty*Ctx;
  133.     four [0][1] = (Stz*Czy-Ctz*Sty*Szy)*Czx-Ctz*Cty*Stx*Szx;
  134.     four [0][2] = 0.0;
  135.     four [0][3] = 0.0;
  136.     four [0][4] = (Stz*Szy+Ctz*Sty*Czy)*Syx+((Stz*Czy-Ctz*Sty*Szy)*Szx
  137.                   +Ctz*Cty*Stx*Czx)*Cyx*P2+(Stz*Szy+Ctz*Sty*Czy)*Cyx
  138.                   -((Stz*Czy-Ctz*Sty*Szy)*Szx+Ctz*Cty*Stx*Czx)*Syx*P1;
  139.  
  140.     four [1][0] = -Stz*Cty*Ctx;
  141.     four [1][1] = (Ctz*Czy+Stz*Sty*Szy)*Czx+Stz*Cty*Stx*Szx;
  142.     four [1][2] = 0.0;
  143.     four [1][3] = 0.0;
  144.     four [1][4] = (Ctz*Szy-Stz*Sty*Czy)*Syx+((Ctz*Czy+Stz*Sty*Szy)*Szx
  145.                   -Stz*Cty*Stx*Czx)*Cyx*P2+(Ctz*Szy-Stz*Sty*Czy)*Cyx-
  146.                   ((Ctz*Czy+Stz*Sty*Szy)*Szx-Stz*Cty*Stx*Czx)*Syx*P1;
  147.  
  148.     four [2][0] = -Sty*Ctx;
  149.     four [2][1] = Sty*Stx*Szx-Cty*Szy*Czx;
  150.     four [2][2] = 0.0;
  151.     four [2][3] = 0.0;
  152.     four [2][4] = (Cty*Czy*Syx-(Cty*Szy*Szx+Sty*Stx*Czx)*Cyx)*P2
  153.                   +(Cty*Czy*Cyx+(Cty*Szy*Szx+Sty*Stx*Czx)*Syx)*P1;
  154.  
  155.     four [3][0] = -Stx;
  156.     four [3][1] = -Ctx*Szx;
  157.     four [3][2] = 0.0;
  158.     four [3][3] = 0.0;
  159.     four [3][4] = Ctx*Czx*Cyx*P2-Ctx*Czx*Syx*P1;
  160.  
  161.     four [4][0] = 0.0;
  162.     four [4][1] = 0.0;
  163.     four [4][2] = 0.0;
  164.     four [4][3] = 0.0;
  165.     four [4][4] = 1.0;
  166.  
  167. /* Cross multipy transformation matrix agaist your points */
  168.  
  169.     for (r=0;r<data;r++)
  170.  {
  171.          for (c=0;c<WID;c++)
  172.       {        
  173.               tweedle = 0.0;
  174.               for (i=0;i<WID;i++)
  175.            {
  176.                    tweedle += (deta [r][i]) * (four [i][c]);
  177.            }
  178.               disp [r][c] = ceil(tweedle);
  179.  
  180.       }
  181.  }
  182. return (TRUE);
  183. }
  184.  
  185. main (argc,argv)
  186. short argc;
  187. char *argv[];
  188. {
  189.  
  190.      double persx,persy;
  191.      short forever = TRUE;
  192.      int x,y,c,pos,top,i,bit,j,zap;
  193.      double pi=3.141592654;
  194.  
  195.      short INTOPEN = FALSE,
  196.            GFXOPEN = FALSE,
  197.            SCNOPEN = FALSE,
  198.            WINOPEN = FALSE,
  199.            MENOPEN = FALSE;
  200.  
  201.      if (argc == 1){ printf ("Please supply an argument bitpattern also ex: four 63 (all planes active)\n");
  202.                     goto HELL;}
  203.  
  204.      bit = atoi(argv[1]); /* get chosen rotations */
  205.  
  206.      if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",INTUITION_REV)))
  207.          goto HELL;INTOPEN = TRUE;
  208.  
  209.      if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",GRAPHICS_REV)))
  210.          goto HELL;GFXOPEN = TRUE;
  211.  
  212.      if (!(Screen = (struct Screen *)OpenScreen(&NewScreen)))
  213.          goto HELL;SCNOPEN = TRUE;
  214.  
  215.      ShowTitle(Screen,TRUE);
  216.      NewWindow.Screen = Screen;
  217.  
  218.      if (!(Window = (struct Window *)OpenWindow(&NewWindow)))
  219.          goto HELL;WINOPEN = TRUE;
  220.  
  221. /* Start of Program */
  222.  
  223.     if (!(top = retrieve ("fourdata"))) goto HELL;
  224.  
  225. /* Main Program Loop */
  226.  
  227.     rads[0]=rads[1]=rads[2]=rads[3]=rads[4]=rads[5]=0.0;
  228.  
  229.     while (forever)
  230.  {
  231.         if (Message = (struct IntuiMessage *)GetMsg(Window->UserPort))
  232.             if ((Message->Class == CLOSEWINDOW)) goto HELL;
  233.     zap=1;
  234.     for (i=0;i<PLA;i++)
  235.  {
  236.         if ((bit & zap))
  237.       {    rads[i]+=.1;
  238.            if (rads[i]>pi) rads[i]=-pi;
  239.       }
  240.         zap=1;
  241.         for (j=0;j<i;j++) zap = zap*2;
  242.  }
  243.  
  244.     persx = 1.0;persy = 1.0;
  245.  
  246.     if (!(multiply (top,persx,persy))) goto HELL;
  247.  
  248.     SetAPen  (Window->RPort,0);
  249.     RectFill (Window->RPort,0,10,640,400);
  250.     SetAPen  (Window->RPort,3);
  251.  
  252.     for (pos=0;pos<(top-1);pos++)
  253.   {
  254.         x = disp [ pos ][ 0 ];
  255.         y = disp [ pos ][ 1 ];
  256.         c = disp [ pos ][ 4 ];
  257.         x += 320;y += 200;
  258.  
  259.         if (pos == 0)
  260.              Move (Window->RPort,x,y);
  261.         Draw (Window->RPort,x,y);
  262.   }
  263.  }
  264. HELL:
  265.  
  266.       if (MENOPEN)  ClearMenuStrip (Window);
  267.       if (WINOPEN)  CloseWindow    (Window);
  268.       if (SCNOPEN)  CloseScreen    (Screen);
  269.       if (GFXOPEN)  CloseLibrary   (GfxBase);
  270.       if (INTOPEN)  CloseLibrary   (IntuitionBase);
  271.       exit (TRUE);
  272. }
  273.